package com.zc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zc.dto.SysRoleDto;
import com.zc.mapper.SysRoleMapper;
import com.zc.mapper.SysRoleResourceMapper;
import com.zc.pojo.SysRole;
import com.zc.pojo.SysRoleResource;
import com.zc.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class SysRoleServiceImpl implements SysRoleService {

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Autowired
    private SysRoleResourceMapper sysRoleResourceMapper;

    @Override
    public List<SysRole> queryAll() {
        return sysRoleMapper.selectAll();
    }

    @Override
    public PageInfo<SysRole> queryByPage(String roleName, Integer page, Integer limit) {
        Map<String, Object> map = new HashMap<>();
        map.put("roleName", roleName);
        PageHelper.startPage(page, limit);
        List<SysRole> sysRoleList = sysRoleMapper.listSysRole(map);
        PageInfo<SysRole> pageInfo = new PageInfo<SysRole>(sysRoleList);

        return pageInfo;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(SysRoleDto sysRoleDto) {
        SysRole sysRole = new SysRole();
        sysRole.setDeleted(new Byte("0"));
        sysRole.setCreateTime(new Date());
        sysRole.setRoleName(sysRoleDto.getRoleName());
        sysRole.setRemark(sysRoleDto.getRemark());
        sysRoleMapper.insert(sysRole);

        //保存角色与资源的关系
        List<Integer> resourceIds = sysRoleDto.getResourceIds();
        for (Integer resourceId : resourceIds) {
            SysRoleResource sysRoleResource=new SysRoleResource();
            sysRoleResource.setResourceId(resourceId.longValue());
            sysRoleResource.setRoleId(sysRole.getRoleId());
            sysRoleResourceMapper.insert(sysRoleResource);
        }
    }

    @Override
    public SysRole getById(Long roleId) {

        return sysRoleMapper.selectByPrimaryKey(roleId);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(SysRoleDto sysRoleDto) {
        SysRole sysRole = new SysRole();
        sysRole.setDeleted(new Byte("0"));
        sysRole.setCreateTime(new Date());
        sysRole.setModifiedTime(new Date());
        sysRole.setRoleName(sysRoleDto.getRoleName());
        sysRole.setRemark(sysRoleDto.getRemark());
        sysRole.setRoleId(sysRoleDto.getRoleId());
        sysRoleMapper.updateByPrimaryKey(sysRole);

        //保存角色与资源的关系
        sysRoleResourceMapper.deleteByRoleId(sysRole.getRoleId());
        List<Integer> resourceIds = sysRoleDto.getResourceIds();
        for (Integer resourceId : resourceIds) {
            SysRoleResource sysRoleResource=new SysRoleResource();
            sysRoleResource.setResourceId(resourceId.longValue());
            sysRoleResource.setRoleId(sysRole.getRoleId());
            sysRoleResourceMapper.insert(sysRoleResource);
        }
    }

    @Override
    public void delete(Long roleId) {
        sysRoleMapper.deleteByPrimaryKey(roleId);
    }

    @Override
    public void logicalDelete(Long roleId) {
        SysRole sysRole = new SysRole();
        sysRole.setDeleted(new Byte("1"));
        sysRole.setModifiedTime(new Date());
        sysRole.setRoleId(roleId);
        sysRoleMapper.logicalDelete(sysRole);
    }
}